package canraid.gui;

import java.util.HashMap;

import canraid.agent.Iagent;
import canraid.config.Properties;
import canraid.debug.Level;
import canraid.debug.Logger;
import canraid.gen.R;
import canraid.gui.listeners.AgentController;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ChatAssistance extends Activity {
	
	private AgentController agentController=AgentController.getInstance();
	private String OFF=null;
	private String ON=null;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Logger.startLog(this);
        Logger.setLog((Object)this,Level.ASSERT,"onCreate().");
        
        setContentView(canraid.gen.R.layout.chat_assistance);
        agentController.doSettings(this, guiHandler);
        OFF=getString(canraid.gen.R.string.off);
        ON=getString(canraid.gen.R.string.on);
        
        Button bBT = (Button) findViewById(canraid.gen.R.id.buttonBluetooth);
        if (bBT != null) {
            bBT.setOnClickListener(agentController);
            bBT.setText(OFF);
        }
        else{
            Logger.setLog((Object)this,Level.WARNING,"onCreate(). ButtonBluetooth not found.");
        }
        
        Button bFile = (Button) findViewById(canraid.gen.R.id.buttonFile);
        if (bFile != null) {
            bFile.setOnClickListener(agentController);
        	bFile.setText(OFF);
        }
        else{
        	Logger.setLog((Object)this,Level.WARNING,"onCreate(). ButtonFile not found.");
        }
        
        Button bNTW = (Button) findViewById(canraid.gen.R.id.buttonNetwork);
        if (bNTW != null) {
        	bNTW.setOnClickListener(agentController);
        	bNTW.setText(OFF);
        }
        else{
        	Logger.setLog((Object)this,Level.WARNING,"onCreate(). ButtonNetwork not found.");
        }
        
        Button bSent = (Button) findViewById(canraid.gen.R.id.button_chat);
        if (bSent != null) {
        	bSent.setOnClickListener(agentController);
        }
        else{
        	Logger.setLog((Object)this,Level.WARNING,"onCreate(). ButtonSent not found.");
        }
        
        EditText editText = (EditText) findViewById(canraid.gen.R.id.textChatSingleLine);
        editText.setText("");
        //editText.setOnEditorActionListener(agentController);
    }	
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		Logger.setLog((Object)this,Level.ASSERT,"onActivityResult().");
		
        agentController.onActivityResult(requestCode, resultCode, data);
    }
	
	public void onDestroy(){
		Logger.setLog((Object)this,Level.ASSERT,"onDestroy().");
		
		agentController.destroy();
		Logger.stopLog();
		super.onDestroy();
		
	}
	
	public final Handler guiHandler = new Handler() {
		private HashMap<Integer, Long> lastRefresh = new HashMap<Integer, Long>();
		
        public void handleMessage(Message msg) {
        	byte[] data=null;
        	int lengthByte=-1;
            String textBps="Bps";
            TextView tBps = null;
            
            switch (msg.what) {
            case Iagent.FROM_BT:
            	data=(byte[]) msg.obj;
            	lengthByte = data.length;
            	
            	if (lengthByte>0 && Properties.MODE==Properties.TECHNICIAN){
	            	EditText editText=(EditText) findViewById(canraid.gen.R.id.textChatMultiline);
	            	editText.append("CAN("+lengthByte+"): "+new String(data)+System.getProperty("line.separator"));
            	}
            	
            	textBps=Integer.toString(lengthByte);
            	//if (lastRefresh.get(Iagent.FROM_BT)!=null)
            	//	textBps=Double.toString((lengthByte*1000)/(System.currentTimeMillis()-lastRefresh.get(Iagent.FROM_BT)));
            	
                tBps = (TextView) findViewById(canraid.gen.R.id.textBluetooth);
                tBps.setText( textBps + " Byte");
                Logger.setLog(this,Level.INFO,"handleMessage(). FROM_BT "+lengthByte+" bytes");
                break;
            case Iagent.SIZE_QUEUE_FILE:
            	lengthByte = (Integer) msg.obj;
            	textBps=Integer.toString(lengthByte);
            	//if (lastRefresh.get(Iagent.SIZE_QUEUE_FILE)!=null)
            	//	textBps = Double.toString((lengthByte*1000)/(System.currentTimeMillis()-lastRefresh.get(Iagent.SIZE_QUEUE_FILE)));
            	
                tBps = (TextView) findViewById(canraid.gen.R.id.textFile);
                tBps.setText( textBps + " Byte");
                Logger.setLog(this,Level.INFO,"handleMessage(). SIZE_QUEUE_FILE "+lengthByte+" bytes");
                break;
            case Iagent.FROM_NTW:
            	data=(byte[]) msg.obj;
            	lengthByte = data.length;
            	textBps=Integer.toString(lengthByte);
            	
            	if (lengthByte>0 && Properties.MODE==Properties.TECHNICIAN){
	            	EditText editText=(EditText) findViewById(canraid.gen.R.id.textChatMultiline);
	            	editText.append("NTW("+lengthByte+"): "+new String(data)+System.getProperty("line.separator"));
            	}
            	
            	//if (lastRefresh.get(Iagent.SIZE_QUEUE_FILE)!=null)
            	//	textBps = Double.toString((lengthByte*1000)/(System.currentTimeMillis()-lastRefresh.get(Iagent.SIZE_QUEUE_FILE)));
            	
                Logger.setLog(this,Level.INFO,"handleMessage(). FROM_NTW "+lengthByte+" bytes");
                break;
            case Iagent.FROM_TEXT:
            	data=(byte[]) msg.obj;
            	lengthByte = data.length;
            	textBps=Integer.toString(lengthByte);
            	
            	if (lengthByte>0 && Properties.MODE==Properties.TECHNICIAN){
	            	EditText editText=(EditText) findViewById(canraid.gen.R.id.textChatMultiline);
	            	editText.append("YOU("+lengthByte+"): "+new String(data)+System.getProperty("line.separator"));
            	}
            	
                Logger.setLog(this,Level.INFO,"handleMessage(). FROM_TEXT "+lengthByte+" bytes");
                break;
            case Iagent.SIZE_QUEUE_NTW:
            	lengthByte = (Integer) msg.obj;
            	textBps=Integer.toString(lengthByte);
            	
            	//if (lastRefresh.get(Iagent.SIZE_QUEUE_FILE)!=null)
            	//	textBps = Double.toString((lengthByte*1000)/(System.currentTimeMillis()-lastRefresh.get(Iagent.SIZE_QUEUE_FILE)));
           
                tBps = (TextView) findViewById(canraid.gen.R.id.textNetwork);
                tBps.setText( textBps + " Byte");
                Logger.setLog(this,Level.INFO,"handleMessage(). SIZE_QUEUE_NTW "+lengthByte+" bytes");
                break;
            case Iagent.ACTION_AGENT_STARTED:
            	Integer startedName = (Integer) msg.obj;
            	Logger.setLog(this,Level.INFO,"handleMessage(). ACTION_AGENT_STARTED: "+startedName);
            	if (startedName==canraid.gen.R.id.buttonBluetooth || startedName==canraid.gen.R.id.buttonFile || startedName==canraid.gen.R.id.buttonNetwork){
            		Button b = (Button) findViewById(startedName);
                    if (b != null) {
                        b.setText(ON);
                    }
            	}
            	break;
            case Iagent.ACTION_AGENT_STOPPED:
            	Integer stoppedName = (Integer) msg.obj;
            	Logger.setLog(this,Level.INFO,"handleMessage().ACTION_AGENT_STOPPED: "+stoppedName);
            	if (stoppedName==canraid.gen.R.id.buttonBluetooth || stoppedName==canraid.gen.R.id.buttonFile || stoppedName==canraid.gen.R.id.buttonNetwork){
            		Button b = (Button) findViewById(stoppedName);
                    if (b != null) {
                        b.setText(OFF);
                    }
            	}
            	break;
            default:
            	Logger.setLog(this,Level.INFO,"handleMessage()."+msg.what+": Type not implemented.");
                break;
            }
            lastRefresh.put(msg.what, System.currentTimeMillis());
        }
	};
	
    public boolean onCreateOptionsMenu(Menu menu) {
    	Logger.setLog((Object)this,Level.ASSERT,"onCreateOptionsMenu().");
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_canraid, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
    	Logger.setLog((Object)this,Level.ASSERT,"onOptionsItemSelected().");
        return agentController.onOptionsItemSelected(item);
    }

}
